home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 400_01 / socketpp-1.5 / test / tsinread.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  1.8 KB  |  103 lines

  1. // tsinread.cc. Test for -*- C++ -*- socket library
  2. // Copyright (C) 1992,1993 Gnanasekaran Swaminathan <gs4t@virginia.edu>
  3. // 
  4. // Permission is granted to use at your own risk and distribute this software
  5. // in source and binary forms provided the above copyright
  6. // notice and this paragraph are preserved on all copies.
  7. // This software is provided "as is" with no express or implied warranty.
  8. //
  9. // Version: 07Nov93 1.5
  10.  
  11. #include <sockinet.h>
  12.  
  13. extern "C" {
  14.     void    sleep(int);
  15. }
  16.  
  17.  
  18. static void process_input (iosockinet& s);
  19.  
  20.  
  21. main()
  22. {
  23.     sockinetbuf    sin (sockbuf::sock_stream);
  24.     
  25.     sin.bind();
  26.     
  27.     cout << "localhost = " << sin.localhost() << endl
  28.      << "localport = " << sin.localport() << endl;
  29.     
  30.     sin.listen();
  31.     
  32.     for(;;) {
  33.     iosockinet    s = sin.accept();
  34.     
  35.     process_input(s);
  36.     s->close();
  37.     
  38.     // lets use select to find out whether a socket is ready
  39.     if (!sin.is_readready(2)) {
  40.         // the socket is not ready. Let us sleep for 1 sec.
  41.         sleep(1);
  42.     }
  43.     
  44.     iosockinet s2 = sin.accept();
  45.     process_input(s2);
  46.     break; // let us get out of forever
  47.     }
  48. }
  49.  
  50. static void process_input(iosockinet& sio)
  51. {
  52.     char        buf[256];
  53.     char*        p = buf;
  54.     
  55.     sio >> p;
  56.     
  57.     while (*p) {
  58.     if (*p != '%')
  59.         cout << *p;
  60.     else
  61.         switch (*++p) {
  62.         case 'd': {
  63.         int i;
  64.         sio >> i;
  65.         cout << i << ' ';
  66.         sio << "INT ";
  67.         break;
  68.         }
  69.         case 'f': {
  70.         double d;
  71.         sio >> d;
  72.         cout << d << ' ';
  73.         sio << "DOUBLE ";
  74.         break;
  75.         }
  76.         case 's': {
  77.         char str[256];
  78.         sio >> str;
  79.         cout << str << ' ';
  80.         sio <<  "STRING ";
  81.         break;
  82.         }
  83.         case 'c': {
  84.         char c;
  85.         sio >> c;
  86.         cout << c << ' ';
  87.         sio << "CHAR ";
  88.         break;
  89.         }
  90.         case '%':
  91.         cout << '%' << ' ';
  92.         break;
  93.         default:
  94.         cout << '%' << *p;
  95.         }
  96.     p++;
  97.     }
  98.     sio << "received\n";
  99.     cout << endl;
  100. }
  101.  
  102.  
  103.